package com.kc.sjwh.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kc.sjwh.dao.ResDao;
import com.kc.sjwh.dao.RoleResDao;
import com.kc.sjwh.entity.po.Res;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;

/**
 * 资源service
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Service
public class ResService {
	static Logger logger = Logger.getLogger(ResService.class);
	@Autowired
	ResDao resDao;
	@Autowired
	RoleResDao roleResDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Res> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t.cjsj desc, t.id");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addCondition("mc", "like", "%"+v+"%");
		}
		v = param.get("yyid");
		if(StringUtils.isNotEmpty(v)) {
			q.put("yyid", v);
		}
		
		return resDao.getPage("getBo", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Res one) {
		resDao.add(one);
	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Res one) {
		one.setGxsj(new Date());
		resDao.update(one, "id");
	}

	/**
	 * 删除
	 * @param id
	 */
	public void delete(String id) {
		//级联删除
		roleResDao.deleteByZyid(id);
		
		int n = resDao.delete(id);
		if(n == 0) {
			throw new IllegalArgumentException("要删除的记录不存在");
		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Res form(String id) {
		return id==null ? null : resDao.getOne("getBo", new QueryParam().put("id", id));
	}

	
	
	
	
	/**
	 * 资源树
	 * @param yyid 所属应用id
	 * @return
	 */
	public List<DictItem> tree(String yyid) {
		List<Res> list = resDao.getList(new QueryParam().put("yyid", yyid).setOrder("pxh,id"));
		List<DictItem> diList = new ArrayList<DictItem>();
		for(Res one : list) {
			DictItem di = new DictItem();
			di.setId(one.getId());
			di.setName(one.getMc());
			di.setPid(one.getFjid());
			diList.add(di);
			
			if(isRoot(list, one)) {
				di.setPid("root");
			}
		}
		
		DictItem root = new DictItem();
		root.setId("root");
		root.setName("资源");
		root.setPid(null);
		diList.add(root);
		
		return diList;
	}
	private boolean isRoot(List<Res> list, Res res) {
		boolean find = false;
		for(Res one : list) {
			if(one.getId().equals(res.getFjid())) {
				find = true;
			}
		}
		return !find;
	}

	public Res getOne(String id) {
		return resDao.getOne("getBo", new QueryParam().put("id", id));
	}

	/**
	 * 保存排序
	 * @param ids
	 */
	public void saveSort(String ids) {
		if(StringUtils.isNotBlank(ids)) {
			int i = 0;
			for(String id : ids.split(",")) {
				Res update = new Res();
				update.setId(id);
				update.setPxh(i++);
				resDao.update(update, "id");
			}
		}
	}
	
}
